package com.edgclub.edg.utils.util;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;

/**
 * 网络工具类
 * 
 * @author eric
 * 
 */
public class NetworkUtility {

	public static final int UNKNOW = 0X00;
	public static final int WIFI = 0X01;
	public static final int MOBILE = 0X02;

	/**
	 * 网络是否可用
	 * 
	 * @param context
	 *            上下文
	 * @return 网络可用则返回true，否则返回false
	 */
	public static boolean isAvailable(Context context) {
		ConnectivityManager cm = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo info = cm.getActiveNetworkInfo();
		return info != null && info.isAvailable();
	}

	/**
	 * 返回Wifi是否启用
	 * 
	 * @param context
	 *            上下文
	 * @return Wifi网络可用则返回true，否则返回false
	 */
	public static boolean isWIFIActivate(Context context) {
		return ((WifiManager) context.getSystemService(Context.WIFI_SERVICE))
				.isWifiEnabled();
	}

	/**
	 * 修改Wifi状态
	 * 
	 * @param context
	 *            上下文
	 * @param status
	 *            true为开启Wifi，false为关闭Wifi
	 */
	public static void changeWIFIStatus(Context context, boolean status) {
		((WifiManager) context.getSystemService(Context.WIFI_SERVICE))
				.setWifiEnabled(status);
	}

	/**
	 * 返回当前连接的网络类型
	 * 
	 * @param context
	 * @return
	 */
	public static final int getNetWorkConnectionType(Context context) {
		ConnectivityManager connectivityManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);

		NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
		if (null != networkInfo && networkInfo.isConnected()) {

			int type = networkInfo.getType();
			if (type == ConnectivityManager.TYPE_WIFI) {
				return WIFI;
			} else if (type == ConnectivityManager.TYPE_MOBILE) {
				return MOBILE;
			} else {
				return UNKNOW;
			}
		}

		return UNKNOW;

	}
}
